package com.test.dao.user;

import com.mysql.jdbc.StringUtils;
import com.test.dao.BaseDao;
import com.test.pojo.Role;
import com.test.pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoTmpl implements UserDao{


    /**
     * 获取用户列表
     * @param connection
     * @param userName
     * @param userRole
     * @param currentPageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    public List<User> getUserList(Connection connection, String userName, int userRole, int currentPageNo, int pageSize) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<User> userList = new ArrayList<User>();
        if(connection != null){
            StringBuffer sql = new StringBuffer();
            sql.append("select u.*,r.roleName as userRoleName from smbms_user u,smbms_role r where  u.userRole=r.id");
            List<Object> list = new ArrayList<Object>();
            if(!StringUtils.isNullOrEmpty(userName)){
                sql.append(" and u.userName like ?");
                list.add("%" + userName + "%"); // index:0
            }
            if(userRole > 0){
                sql.append(" and u.userRole=?");
                list.add(userRole); // index:1
            }
            sql.append(" order by creationDate desc limit ?,?");
            currentPageNo = (currentPageNo-1)*pageSize;
            list.add(currentPageNo);
            list.add(pageSize);
            Object[] params = list.toArray();
            System.out.println(sql);
            // 获取结果集
            resultSet = BaseDao.execute(connection, sql.toString(), params, resultSet, preparedStatement);
            while (resultSet.next()){
                User _user = new User();
                _user.setId(resultSet.getInt("id"));
                _user.setUserCode(resultSet.getString("userCode"));
                _user.setUserName(resultSet.getString("userName"));
                _user.setGender(resultSet.getInt("gender"));
                _user.setBirthday(resultSet.getDate("birthday"));
                _user.setPhone(resultSet.getString("phone"));
                _user.setUserRole(resultSet.getInt("userRole"));
                _user.setUserRoleName(resultSet.getString("userRoleName"));
                userList.add(_user);
            }
            // 释放资源
            BaseDao.closeResource(null,resultSet,preparedStatement);
        }
        return userList;
    }

    /**
     * 查询用户总数 - 根据用户名或者用户角色
     * @param connection
     * @param userName
     * @param userRole
     * @return
     * @throws SQLException
     */
    public int getUserCount(Connection connection, String userName, int userRole) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        int count = 0;
        if(connection != null){
            // sql
            StringBuffer sql = new StringBuffer();
            sql.append("select count(1) as count from smbms_user u,smbms_role r where u.userRole=r.id");
            ArrayList<Object> list = new ArrayList<Object>();//存放参数
            if(!StringUtils.isNullOrEmpty(userName)){
                sql.append(" and u.userName like ?");
                list.add("%" + userName + "%"); // index:0
            }
            if(userRole > 0){
                sql.append(" and u.userRole=?");
                list.add(userRole); // index:1
            }
            // list转化成数组
            Object[] params = list.toArray();
            System.out.println(sql.toString()); //输出完整的sql语句
            // 获取结果集
            resultSet = BaseDao.execute(connection, sql.toString(), params, resultSet, preparedStatement);
            if(resultSet.next()){
                count = resultSet.getInt("count"); //从结果集中获取数量
            }
            // 释放资源
            BaseDao.closeResource(null,resultSet,preparedStatement);
        }



        return count;
    }

    /**
     * 修改用户密码
     * @param connection
     * @param id
     * @param userPassword
     * @return
     * @throws SQLException
     */
    public int updatePwd(Connection connection, int id, String userPassword) throws SQLException {
        PreparedStatement preparedStatement = null;
        // sql
        String sql = "update smbms_user set userPassword=? where id=?";
        Object[] params = {userPassword,id};
        int updateRows = 0;
        if(connection != null){
            updateRows = BaseDao.execute(connection,sql,params,preparedStatement);
            // 释放资源
            BaseDao.closeResource(null,null,preparedStatement);
        }
        return updateRows;
    }

    /**
     * 获取登陆的用户
     * @param connection
     * @param userCode
     * @return
     * @throws SQLException
     */
    public User getLoginUser(Connection connection, String userCode) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user = null;
        // sql
        String sql = "select * from smbms_user where userCode=?";
        Object[] params = {userCode};
        // 获取结果集
        resultSet = BaseDao.execute(connection, sql, params, resultSet, preparedStatement);
        //给实体类赋值
        if(resultSet.next()){
            user = new User();
            user.setId(resultSet.getInt("id"));
            user.setUserCode(resultSet.getString("userCode"));
            user.setUserName(resultSet.getString("userName"));
            user.setUserPassword(resultSet.getString("userPassword"));
            user.setGender(resultSet.getInt("gender"));
            user.setBirthday(resultSet.getDate("birthday"));
            user.setPhone(resultSet.getString("phone"));
            user.setAddress(resultSet.getString("address"));
            user.setUserRole(resultSet.getInt("userRole"));
            user.setCreatedBy(resultSet.getInt("createdBy"));
            user.setCreationDate(resultSet.getDate("creationDate"));
            user.setModifyBy(resultSet.getInt("modifyBy"));
            user.setModifyDate(resultSet.getDate("modifyDate"));
        }
        // 释放资源
        BaseDao.closeResource(null,resultSet,preparedStatement);

        return user;
    }
}
